package com.wnhz.work.jy;

import android.app.Application;
import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Typeface;
import android.util.Log;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.solidfire.gson.Gson;
import com.tencent.mm.sdk.openapi.IWXAPI;
import com.tencent.mm.sdk.openapi.WXAPIFactory;
import com.umeng.socialize.PlatformConfig;
import com.umeng.socialize.UMShareAPI;
import com.uuzuche.lib_zxing.activity.ZXingLibrary;
import com.wnhz.work.jy.bean.UserBean;
import com.wnhz.work.jy.bean.WeiXinBean1;
import com.wnhz.work.jy.bean.WeiXinMessageBean;
import com.wnhz.work.jy.utils.ConfigUtil;
import com.wnhz.work.jy.utils.xutils3.MyCallBack;
import com.wnhz.work.jy.utils.xutils3.XUtil;

import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.image.ImageOptions;
import org.xutils.x;

import java.util.HashMap;
import java.util.Map;

import io.rong.imkit.RongIM;


/**
 * Created by Administrator on 2016/9/6 0006.
 */
public class MyApplication extends Application {
    private static final String TAG = "MyApplication";
    public static IWXAPI api;
    public int isWXPay = 100;
    private static MyApplication instance;
    public static Typeface typeFace;
    public WeiXinBean1 bean1;
    public WeiXinMessageBean bean;

    public UserBean userBean;

    public Map<String, Object> sign_map;

    private String phone;
    private String passWork;

    public boolean is_start_login;


    public ImageOptions headoptions = new ImageOptions.Builder()
            //设置加载过程中的图片
            .setLoadingDrawableId(R.mipmap.ic_morentouxiang)
            //设置加载失败后的图片
            .setFailureDrawableId(R.mipmap.ic_morentouxiang)
            //设置使用缓存
            .setUseMemCache(true)
            //设置显示圆形图片
            .setCircular(true)
            //设置支持gif
            .setIgnoreGif(false)
            .build();

    @Override
    public void onCreate() {
        // TODO Auto-generated method stub
        super.onCreate();

        instance = this;
        initImageLoader();
//        typeFace =Typeface.createFromAsset(getAssets(),"fonts/Didot.ttf");
        //注册微信
        weixinLoading();
        // 微信 appid appsecret
        PlatformConfig.setWeixin("wx054406f5ccd828da", "edd22fe6298e1176df89baeeaf421284");
        // 新浪微博 appkey appsecret
        //PlatformConfig.setSinaWeibo("3921700954", "04b48b094faeb16683c32669824ebdad");
        // QQ和Qzone appid appkey
        PlatformConfig.setQQZone("1105763186", "TUoJYFOwCGMEdglH");
        UMShareAPI.get(this);
        RongIM.init(this);
        x.Ext.init(this);
        upUser();
        ZXingLibrary.initDisplayOpinion(this);
        // 注册全局异常处理
        //CrashHandler.getInstance().init(this);
    }

    private void weixinLoading() {
        api = WXAPIFactory.createWXAPI(this, "wx054406f5ccd828da", true);
        api.registerApp("wx054406f5ccd828da");
    }


    public static MyApplication getInstance() {
        return instance;
    }

    /**
     * 配置ImageLoader
     */
    private void initImageLoader() {

        // 显示相关的配置
        DisplayImageOptions imageOptions = new DisplayImageOptions.Builder()
                // 配置Url加载错误的图片

                .showImageForEmptyUri(R.mipmap.img_bannerzhanweitu).showImageOnFail(R.mipmap.img_bannerzhanweitu)
                .showImageOnLoading(R.mipmap.img_bannerzhanweitu)
                .cacheInMemory(true)// 设置下载的图片是否缓存在内存中
                .cacheOnDisc(true)// 设置下载的图片是否缓存在SD卡中

                .build();

        // 性能，内存配置
        ImageLoaderConfiguration configuration = new ImageLoaderConfiguration.Builder(instance).threadPoolSize(3) // 配置2条线程
                .threadPriority(Thread.NORM_PRIORITY - 2) // 配置线程优先级，比UI线程低，这样更流畅点
                .memoryCacheSize(10 * 1024 * 1024) // 配置内存缓存，值为5M
                .defaultDisplayImageOptions(imageOptions) // 默认的显示相关配置。使用的时候也可以在display中使用其他的option来配置
                .build();

        ImageLoader.getInstance().init(configuration);

    }


    public void addUser(String phone, String passwork) {
        SharedPreferences sp = getSharedPreferences("user", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sp.edit();
        editor.putString("phone", phone);
        editor.putString("passwork", passwork);
        editor.commit();
    }

    public void removerUser() {
        SharedPreferences sp = getSharedPreferences("user", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sp.edit();
        editor.remove("phone");
        editor.remove("passwork");
        phone = "";
        passWork = "";
        editor.commit();
    }

    private void getUserPhone() {
        SharedPreferences sp = getSharedPreferences("user", Context.MODE_PRIVATE);
        phone = sp.getString("phone", "");
        passWork = sp.getString("passwork", "");
    }

    public void upUser() {
        getUserPhone();
        Map<String, Object> map = new HashMap<>();
        if (!"".equals(phone) && !"".equals(passWork)) {
            map.put("phoneNumber", phone);
            map.put("password", passWork);

        } else {
            if (bean1 == null) {
                return;
            }
            map.put("openId", bean1.getOpenid());
            map.put("unionId", bean1.getUnionid());
        }
        XUtil.Post(ConfigUtil.LOGIN, map, new MyCallBack<String>() {
            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                Log.e(TAG, "onSuccess: " + result);
                try {
                    JSONObject jsonObject = new JSONObject(result);
                    String re = jsonObject.optString("result");
                    if ("1".equals(re)) {
                        String r = jsonObject.optString("info");
                        userBean = new Gson().fromJson(r, UserBean.class);
                        Log.e(TAG, "onSuccess: " + userBean.toString());
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }


            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                super.onError(ex, isOnCallback);
                Log.d(TAG, "onError: "+ex);
            }

            @Override
            public void onFinished() {
                super.onFinished();
            }
        });
    }


}
